<#--Form-->
<#macro Form action id="" onsubmit="return false;" target="" namespace="" upload=false class="">
    <#assign CurrentFormAction=action/>
    <#if id=="">
        <#assign CurrentFormId="Form_${GetUniqueIndex()}">
    <#else>
        <#assign CurrentFormId=id/>
    </#if>
    <#assign CurrentForm="$('${CurrentFormId}')"/>
    <#if upload>
        <#assign CurrentFormTarget="/"/>
    <#else>
        <#assign CurrentFormTarget=target/>
    </#if>
    <form id="${CurrentFormId}" action="${action}" onsubmit="${onsubmit}" method="POST"
        <#if upload>enctype="multipart/form-data" target="#FrameTargetIframe"</#if> class="${class}">
        <#if upload><input type="hidden" name="_target_uplaod_" value="${target}"></#if>
        <#nested>
    </form>
	<script type="text/javascript">
	$(document).ready(function() { 
	    var ${CurrentFormId}_options = { 
	        //target:        '#${CurrentFormTarget}'   // target element(s) to be updated with server response 
	        //beforeSubmit:  showRequest  // pre-submit callback 
	        //success:       showResponse  // post-submit callback 
	 
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    }; 
	    $('#${CurrentFormId}').ajaxForm(${CurrentFormId}_options); 
	}); 
	</script>
</#macro>

<#-- FieldError -->
<#macro FieldError label="">
    <#if fieldErrors??><div class="FieldError">
        <#if label=="">
            <#list fieldError?keys as key>
                field[${key}]error:${fieldErrors[key]?html}<br>
            </#list>
        <#elseif fieldErrors[label]??>
            <#list fieldErrors["${label}"] as err>
                ${err?html}<br>
                <#assign FoundFieldErrors=true/>
            </#list>
        </#if></div>
    </#if>
</#macro>

<#-- InputCommons -->
<#macro InputCommons id label name>
    <#assign MyLabel="!${label}"/>
    <#if label==""><#assign MyLabel=name/></#if>
    <#if id=="">
        <#assign MyId="MyId_${GetUniqueIndex()}"/>
    <#else>
        <#assign MyId=id/>
    </#if>
    <#nested>
</#macro>

<#-- private method-->
<#macro FieldMayBeInTable name label required=false inrow=true>
    <#if CurrentTableType?? && CurrentTableType=="EditTable">
        <#if inrow && (!CurrentInRow?? || !CurrentInRow)><tr></#if>
        <th class="FieldLabel"><nobr><#if label!="" && label!=".">${bundle(label)}<span style="color:#A03202;">${required?string("&nbsp;*","")}</span></nobr></#if></th>
        <td class="FieldEdit"><nobr><#nested></nobr></td>
        <td class="FieldError"><@FieldError label=name/></td>
        <#if inrow && (!CurrentInRow?? || !CurrentInRow)></tr></#if>
    <#else>
        <@FieldError label=name/>
        <#if label!="." && label !="">${bundle(label)}<span style="color:#A03202;">${required?string("&nbsp;*","")}</span></#if>
        <#nested>
    </#if>
</#macro>

<#-- text display only -->
<#macro TextField id="" name="" label="" value="" img="" inrow=true tohtml=true unit=".">
	<@InputCommons id=id name=name label=label>
	<@FieldMayBeInTable label=MyLabel name=name required=required inrow=inrow>
	    <#if tohtml>&nbsp;${value?html}&nbsp;${bundle(unit)}<#else>&nbsp;${value}&nbsp;${bundle(unit)}</#if>
	    <#nested>
	    <#if name!=""><input type="hidden" name="${name}" value="${value?html}"></#if>
    </@FieldMayBeInTable>
    </@InputCommons>
</#macro>

<#-- Input -->
<#macro Input id="" name="" label="" value="" values={} type="text" retain=true
        onclick="" unit="." tooltip="" required=false inrow=true style="" class="" onblur="" onfocus="" disabled="" readonly=false>
    <#if type=="hidden">
        <input type="hidden" name="${name}" value="${value!''}">
    <#else>
        <@InputCommons id=id name=name label=label>
        <@FieldMayBeInTable label=MyLabel name=name required=required inrow=inrow>
            <input id="${MyId}" type="${type}" name="${name}" <#if class!="">class="${type}Edit ${class}"<#else>class="${type}Edit"</#if>
                    value="<#if retain && fieldErrors?? && fieldErrors[name]?? && Parameters[name]??>${Parameters[name]?html}<#elseif value??>${value?html}</#if>"
                <#if style!="">style="${style}"</#if> 
                <#if disabled=="true">disabled=true</#if>
                <#if readonly>readonly="readonly"</#if>
                <#if onfocus!="">onfocus="${onfocus}"</#if>
                <#if onblur!="">onblur="${onblur}"</#if><#nested>>&nbsp;&nbsp;${bundle(unit)}
        </@FieldMayBeInTable>
        </@InputCommons>
    </#if>
</#macro>

<#-- Date -->
<#macro Date id="" name="" label="" value="" onclick="" unit="." required=false inrow=true style="" disabled="" onfocus="">
	<@Input id=id name=name label=label value=value onclick=onclick readonly=true class="Wdate" style=style onfocus=onfocus/>
</#macro>

<#-- checkbox -->
<#macro Checkbox name label="" prelabel="" value="true" checked=true id="" style="" onclick="" class="" tooltip="" required=false inrow=true>
	<#assign MyPrelabel="!${prelabel}"/>
    <@InputCommons id=id name=name label=label>
    <@FieldMayBeInTable label=MyPrelabel name=name required=required inrow=inrow>
            <input id="${MyId}" type="checkbox" name="${name}" value="${value}" onclick="${onclick}" <#if checked>checked</#if> <#if style!="">style="${style}"</#if> class="checkboxEdit" <#nested>>
            ${bundle(MyLabel)}
    </@FieldMayBeInTable>
    </@InputCommons>
</#macro>

<#-- radio -->
<#macro Radio id="" name="" label="" value="" values={} style="" class="" onclick="" split="&nbsp;" tooltip="" required=false inrow=true disabled=false>
    <@InputCommons id=id name=name label=label>
    <@FieldMayBeInTable label=MyLabel name=name required=required inrow=inrow>
            <#list values?keys as k>
                <#if k_index gt 0>${split}</#if>
                <input type="radio" name="${name}" value="${values[k]!''}" <#if values[k]==value>checked</#if> <#if disabled>disabled</#if>
                    <#if onclick!="">onclick="var value='${(values[k]!'')?js_string}';${onclick}"</#if>>${k}
            </#list>
    </@FieldMayBeInTable>
    </@InputCommons>
</#macro>

<#-- textarea -->
<#macro Textarea name id="" label="" rows=3 cols=20 style="" class="" onclick="" tooltip="" required=false inrow=true>
    <@InputCommons id=id name=name label=label>
    <@FieldMayBeInTable label=MyLabel name=name required=required inrow=inrow>
        <textarea id="${MyId}" name="${name}" cols="${cols}" rows="${rows}" onclick="${onclick}" class="textEdit"><#nested></textarea>
    </@FieldMayBeInTable>
    </@InputCommons>
</#macro>

<#-- Multi select -->
<#macro MultiSelect name label="" values=[] map={} id="" usebundle=false style="width:150px" size="5" onchange="" unit="." required=false tooltip="" inrow=true class="">
    <@Select type="multi" id=id name=name label=label values=values map=map usebundle=usebundle
            style=style size=size onchange=onchange unit=unit required=required inrow=inrow tooltip=tooltip class=class>
            <#nested>
    </@Select>
</#macro>

<#-- select -->
<#macro Select id="" type="" name="" label="" value="" style="" class="" map={} values=[]
        usebundle=false onchange="" readonly=false unit="." tooltip="" required=false inrow=true size="">
    <@InputCommons id=id name=name label=label>
    <#assign SelectedValue=value/>
    <@FieldMayBeInTable label=MyLabel name=name required=required inrow=inrow>
        <select id="${MyId}" name="${name}" value="${value?html}" <#if type=="multi">size="${size}"</#if>
            <#if SelectedValue !="" || style !="">style="${style}"<#else>style="width:154px;"</#if> class="${class}" onchange="${onchange}" <#if readonly>disabled</#if>>
            <#assign UseBundle=(usebundle && type!="combo")/>
            <#list map?keys as key>
                <@Option value=key label=map[key] usebundle=UseBundle></@Option>
            </#list>
            <#list values as v>
                <@Option value=v label=v usebundle=UseBundle/>
            </#list>
            <#nested>
        </select>&nbsp;${bundle(unit)}
    </@FieldMayBeInTable>
    </@InputCommons>
</#macro>

<#macro Option value label="" selected=true usebundle=false>
    <option value="${value?html}" <#if selected && (value?? && value==SelectedValue)>selected="selected"<#else></#if>><#if usebundle>${bundle(label)}<#else>${label?html}</#if><#nested></option>
</#macro>

<#-- Submit -->
<#macro Submit label action="" onclick="" target="" id="" disabled=false params="{}" tooltip="." progressdialog="" autodisabled=true>
    <#if action=="">
        <#assign MyAction="${CurrentForm}.action='${CurrentFormAction}';"/>
    <#else>
        <#assign MyAction="${CurrentForm}.action='${action}';"/>
    </#if>
    <#if target =="">
        <#if !CurrentFormTarget?? || CurrentFormTarget =="">
            <#assign MyTarget=CurrentTarget()/>
        <#else>
            <#assign MyTarget=",'${CurrentFormTarget}'"/>
        </#if>
    <#else>
        <#assign MyTarget=",'${target}'"/>
    </#if>
    <@Button id=id label=label onclick="${MyAction}${onclick};base.submit('${CurrentFormId}'${MyTarget},${params});"
            disabled=disabled tooltip=tooltip autodisabled=autodisabled/>
</#macro>

<#macro Button label id="" onclick="" style="" type="" href="" target="" disabled=false params="{}" tooltip="." autodisabled=true>
    <@InputCommons id=id name=label label=label>
        <button id="${MyId}" style="${style}" <#if disabled>disabled</#if>
            onclick="try{${onclick};}finally{}; <@LoadUrl href=href target=target params=params/>" >
            <span>${bundle(MyLabel)}</span>
        </button>
    </@InputCommons>
</#macro>
